#include "trans.h"

#include <stddef.h>

#include "sqlite3.h"

#include "log/log.h"

#include "tags.h"

static int trans_op_exec(sqlite3* db, const char* exec, const char* sql) {
	sqlite3_stmt* statement = NULL;
	int ret = sqlite3_prepare_v2(db, sql, -1, &statement, 0);

//	log_print(ANDROID_LOG_DEBUG, TAG_NIM_INDEX_OP, "trans_op_exec %s : prepare %d", exec, ret);

	if (ret != SQLITE_OK) {
		return ret;
	}

	ret = sqlite3_step(statement);
//	log_print(ANDROID_LOG_DEBUG, TAG_NIM_INDEX_OP, "trans_op_exec %s : step %d", exec, ret);

	sqlite3_finalize(statement);

	return ret;
}

int trans_op(sqlite3* db, int op) {
	if (op == 0) {
		return trans_op_exec(db, "BEGIN", "BEGIN");
	}

	if (op == 1) {
		return trans_op_exec(db, "COMMIT", "COMMIT");
	}

	if (op == 2) {
		return trans_op_exec(db, "ROLLBACK", "ROLLBACK");
	}

	return SQLITE_DONE;
}
